package cn.edu.web;

import cn.edu.pojo.*;
import cn.edu.service.InfomationService;
import cn.edu.service.TeacherService;
import cn.edu.service.impl.InfomationServiceImpl;
import cn.edu.service.impl.TeacherServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author Davey chan
 */
@WebServlet("/Infomation/*")
public class InfomationServlet extends BaseServlet {
    private InfomationService tService = new InfomationServiceImpl();


    //分页查询
    public void infoselect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        处理中文乱码
        request.setCharacterEncoding("utf-8");
//接受当前页码，和每页展示条数
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        String _academyName = request.getParameter("academyName");
        byte[]  bytes = _academyName.getBytes("iso-8859-1");
        String  academyName = new String(bytes,"utf-8");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize= Integer.parseInt(_pageSize);
//获取查询的封装对象

        BufferedReader br=request.getReader();
        String s = br.readLine();

        //        将JSON字符串转成JAVA对象
        All t = JSON.parseObject(s,All.class);

        InfoPageBean<All> pageBean = tService.infoselect(currentPage, pageSize,academyName,t);


//将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(pageBean);
//        响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //  获取饼图的数据
    public void getPie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的学院的信息
        String _academyName = request.getParameter("academyName");
        byte[]  bytes = _academyName.getBytes("iso-8859-1");
        String  academyName = new String(bytes,"utf-8");

//        调用Service的同意
        List<PieData> pie = tService.getpie(academyName);
        String jsonString = JSON.toJSONString(pie);
//        响应数据
        response.setContentType("text/json;charset=utf-8");
//        返回成功的标识
        response.getWriter().write(jsonString);


    }
    //  获取柱状图的数据
    public void getBar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的学院的信息
        String _academyName = request.getParameter("academyName");
        byte[]  bytes = _academyName.getBytes("iso-8859-1");
        String  academyName = new String(bytes,"utf-8");
//        调用Service的同意
        List<Integer> bar = tService.getBar(academyName);
        String jsonString = JSON.toJSONString(bar);
//        响应数据
        response.setContentType("text/json;charset=utf-8");
//        返回成功的标识
        response.getWriter().write(jsonString);


    }

}
